package com.xiaomi.back.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaomi.back.service.describes.DescribesBack;
import com.xiaomi.back.service.malltype.MallService;
import com.xiaomi.pojo.Describes;
import com.xiaomi.pojo.Malltype;
import com.xiaomi.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class DescribesController {

    @Autowired
    private DescribesBack describesBack;
    @Autowired
    private MallService mallService;


    //商品查询 库存
    @RequestMapping("/GetComm")
    public String GetComm(Model model, @RequestParam(defaultValue = "1") int currentPageNo) {
        PageHelper.startPage(currentPageNo, 8);
        //在售所有商品
        List<Describes> describes = describesBack.getAllDescribes(1);
        PageInfo<Describes> page = new PageInfo<>(describes);
        model.addAttribute("page", page);

        //查询商品类型
        List<Malltype> malltypes=mallService.getMallType();
        model.addAttribute("malltypes",malltypes);

        //查询商品加购页面商品推荐个数
        int result=describesBack.getAddcart();
        model.addAttribute("result",result);
        return "back/Comm/commList";
    }


    //根据商品名称 查询上架商品
    @RequestMapping("/listDescribes")
    public String listDescribes(@RequestParam(defaultValue = "1") int currentPageNo,@RequestParam(defaultValue = "0") int lx, Model model, @RequestParam("name") String comname) {
        PageHelper.startPage(currentPageNo, 8);
        List<Describes> describes = describesBack.GetDescribes(comname,lx);
        PageInfo<Describes>page=new PageInfo<>(describes);
        model.addAttribute("page", page);
        model.addAttribute("name",comname);
        model.addAttribute("lx",lx);

        //查询商品加购页面商品推荐个数
        int result=describesBack.getAddcart();
        model.addAttribute("result",result);

        return "back/Comm/commFind";
    }


    //下架  商品查询
    @RequestMapping("/shelvesComm")
    public String shelvesComm(Model model, @RequestParam(defaultValue = "1") int currentPageNo) {
        PageHelper.startPage(currentPageNo, 11);
        //在售所有商品
        List<Describes> describes = describesBack.getAllDescribes(2);
        PageInfo<Describes> page = new PageInfo<>(describes);
        model.addAttribute("page", page);

        //查询商品类型
        List<Malltype> malltypes=mallService.getMallType();
        model.addAttribute("malltypes",malltypes);

        return "back/Comm/shelveslist";
    }

    //根据商品名称 查询下架商品
    @RequestMapping("/getShelvesComm")
    public String getShelvesComm(@RequestParam(defaultValue = "1") int currentPageNo, Model model, @RequestParam("name") String comname,@RequestParam(defaultValue = "0") int lx) {
        PageHelper.startPage(currentPageNo, 11);
        List<Describes> describes = describesBack.GetComms(comname,lx);
        System.out.println(describes.toString());
        PageInfo<Describes>page=new PageInfo<>(describes);
        model.addAttribute("page", page);
        model.addAttribute("name",comname);
        model.addAttribute("lx",lx);
        return "back/Comm/shelvesFind";
    }

    //取消 添加 购物车推荐
    @RequestMapping("/addcart")
    public String AddCancel(@RequestParam("add")int add,@RequestParam("deid")int cid,Model model){
        int result=describesBack.UpcartRecomm(add,cid);
        model.addAttribute("result",result);
        return "back/Comm/Result";
    }

    //显示购物车 推广商品
    @RequestMapping("/getCartRedcomm")
    public String getCartRedcomm(Model model){
        List<Describes> describes=describesBack.getTui();
        int result=describes.size();
        model.addAttribute("result",result);
        model.addAttribute("describes",describes);
        return "back/Comm/cartRedcomm";
    }
}













